package com.zhehekeji.opsys.api.service.manager;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhehekeji.opsys.api.code.LuckySigninResultCode;
import com.zhehekeji.opsys.api.common.util.DateFormatUtil;
import com.zhehekeji.opsys.api.conf.Config;
import com.zhehekeji.opsys.api.enm.CardDetailStatus;
import com.zhehekeji.opsys.api.enm.EpgType;
import com.zhehekeji.opsys.api.enm.PrizeType;
import com.zhehekeji.opsys.api.entity.TCardDetail;
import com.zhehekeji.opsys.api.entity.TCardInfo;
import com.zhehekeji.opsys.api.entity.TSignin;
import com.zhehekeji.opsys.api.entity.TSigninExchange;
import com.zhehekeji.opsys.api.entity.TSigninPicture;
import com.zhehekeji.opsys.api.entity.TSigninPrize;
import com.zhehekeji.opsys.api.entity.TSigninProduct;
import com.zhehekeji.opsys.api.entity.TSigninRecord;
import com.zhehekeji.opsys.api.entity.TSigninRecordDay;
import com.zhehekeji.opsys.api.entity.TSigninUser;
import com.zhehekeji.opsys.api.resp.GetSigninExchangeListResp;
import com.zhehekeji.opsys.api.resp.GetSigninPictureListResp;
import com.zhehekeji.opsys.api.resp.GetSigninPictureResp;
import com.zhehekeji.opsys.api.resp.GetSigninPrizeDetailResp;
import com.zhehekeji.opsys.api.resp.GetSigninPrizeListResp;
import com.zhehekeji.opsys.api.resp.GetSigninProductDetailResp;
import com.zhehekeji.opsys.api.resp.GetSigninProductListResp;
import com.zhehekeji.opsys.api.resp.GetSigninRecordResp;
import com.zhehekeji.opsys.api.resp.LuckySigninResp;
import com.zhehekeji.opsys.api.service.CommonService;
import com.zhehekeji.opsys.api.service.validate.SigninValidate;
import com.zhehekeji.opsys.api.service.validate.ValidateRets;
import com.zhehekeji.opsys.api.service.validate.args.SigninValidateArgs;

/**
 * @ClassName SigninManagerService
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-7-13   上午10:55:22
 */
@Service
public class SigninManagerService extends CommonService {
	
	private final static Logger logger = LoggerFactory.getLogger(SigninManagerService.class);
	
	@Autowired
	private Config config;
	@Autowired
	private SigninValidate signinValidate;
	
	public GetSigninPictureResp getSigninPicture(String pictureCode) {
		
		GetSigninPictureResp resp = new GetSigninPictureResp();
		if (StringUtils.isEmpty(pictureCode)){
			logger.error("图片编码非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("图片编码非法！");
			return resp;
		}
		try {
			TSigninPicture tmpTSigninPicture = entityDao.get(TSigninPicture.class, "o.code=?1", new Object[]{pictureCode});
			if (tmpTSigninPicture == null){
				logger.error("图片不存在，图片编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("图片不存在，图片编码非法！");
				return resp;
			}
			Integer tmpStatus = tmpTSigninPicture.getStatus();
			if (tmpStatus == 0){
				logger.error("图片已经停用！");
				resp.setResultCode(LuckySigninResultCode.PICTURE_HAS_BEEN_STOPPED);
				resp.setResultDesc("图片已经停用！");
				return resp;
			}
			resp.setAboveTime(tmpTSigninPicture.getAboveTime());
			resp.setCode(pictureCode);
			resp.setCpCode(tmpTSigninPicture.getCpCode());
			resp.setName(tmpTSigninPicture.getName());
			if (!StringUtils.isEmpty(tmpTSigninPicture.getPicture())){
				resp.setPicture(getHttpUrl(tmpTSigninPicture.getPicture()));
			}
			resp.setProfile(tmpTSigninPicture.getProfile());
			logger.info("获取图片信息成功！");
		} catch (Exception e) {
			logger.error("获取图片信息失败，程序异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取图片信息失败，程序异常！");
		}
		return resp;
		
	}

	public GetSigninPictureResp getLatestSigninPicture(String signinCode) {
		
		GetSigninPictureResp resp = new GetSigninPictureResp();
		try {
			//取最新创建的图片
			if (StringUtils.isBlank(signinCode)){
				logger.error("签到编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("签到编码非法！");
				return resp;
			}
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("createTime", "desc");
			List<TSigninPicture> tmpListTSigninPicture = entityDao.getScrollData(TSigninPicture.class, 0, 1, "o.tsignin.code=?1 and o.status=1", new Object[]{signinCode}, orderBy);
			if (tmpListTSigninPicture != null && !tmpListTSigninPicture.isEmpty()){
				TSigninPicture tmpTSigninPicture = tmpListTSigninPicture.get(0);
				resp.setAboveTime(tmpTSigninPicture.getAboveTime());
				resp.setCode(tmpTSigninPicture.getCode());
				resp.setCpCode(tmpTSigninPicture.getCpCode());
				resp.setName(tmpTSigninPicture.getName());
				if (!StringUtils.isEmpty(tmpTSigninPicture.getPicture())){
					resp.setPicture(getHttpUrl(tmpTSigninPicture.getPicture()));
				}
				resp.setProfile(tmpTSigninPicture.getProfile());
			}
			logger.info("获取图片信息成功！");
		} catch (Exception e) {
			logger.error("程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("程序异常");
		}
		return resp;
		
	}

	public GetSigninPictureListResp getSigninPictureList(String signinCode, int firstIndex, int maxResult) {
		
		GetSigninPictureListResp resp = new GetSigninPictureListResp();
		try {
			if (StringUtils.isBlank(signinCode)){
				logger.error("签到编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("签到编码非法！");
				return resp;
			}
			String tmpCountHql = "select count(o.id) as count from TSigninPicture o where o.tsignin.code=? and o.status=1";
			List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpCountHql, new Object[]{signinCode}, -1, -1);
			int tmpIntCount = 0;
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				Long tmpLongCount = (Long) tmpMapCount.get("count");
				tmpIntCount = tmpLongCount.intValue();
			}
			if(tmpIntCount <= 0) return resp;
			 
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("createTime", "asc");
			List<TSigninPicture> tmpListTSigninPicture = entityDao.getScrollData(TSigninPicture.class, firstIndex, maxResult, 
					"o.tsignin.code=?1 and o.status=1", new Object[]{signinCode}, orderBy);
			if (tmpListTSigninPicture == null || tmpListTSigninPicture.isEmpty()) return resp;
			List<GetSigninPictureListResp.SigninPictureInfo> tmpListSigninPictureInfo = new ArrayList<GetSigninPictureListResp.SigninPictureInfo>();
			GetSigninPictureListResp.SigninPictureInfo tmpSigninPictureInfo;
			for (TSigninPicture tmpTSigninPicture : tmpListTSigninPicture){
				tmpSigninPictureInfo = new GetSigninPictureListResp.SigninPictureInfo();
				tmpSigninPictureInfo.setAboveTime(tmpTSigninPicture.getAboveTime());
				tmpSigninPictureInfo.setCode(tmpTSigninPicture.getCode());
				tmpSigninPictureInfo.setCpCode(tmpTSigninPicture.getCpCode());
				tmpSigninPictureInfo.setName(tmpTSigninPicture.getName());
				if (!StringUtils.isEmpty(tmpTSigninPicture.getPicture())){
					tmpSigninPictureInfo.setPicture(getHttpUrl(tmpTSigninPicture.getPicture()));
				}
				tmpSigninPictureInfo.setProfile(tmpTSigninPicture.getProfile());
				
				tmpListSigninPictureInfo.add(tmpSigninPictureInfo);
			}
			resp.setSigninPictureInfos(tmpListSigninPictureInfo);
			resp.setRecordSize(tmpIntCount);
			logger.info("获取图片列表成功！");
		} catch (Exception e) {
			logger.error("获取图片列表失败，程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取图片列表失败，程序异常！");
		}
		return resp;
		
	}

	public GetSigninPictureResp getSigninPictureByAboveTime(String signinCode, String aboveTime, int epgType) {
		
		GetSigninPictureResp resp = new GetSigninPictureResp();
		if (StringUtils.isBlank(signinCode)){
			logger.error("签到编码非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("签到编码非法！");
			return resp;
		}
		try {
			try{
				DateUtils.parseDate(aboveTime, new String[]{"yyyy-MM-dd"});
			} catch (ParseException e){
				logger.error("获取图片信息失败，参数非法（要求格式yyyy-MM-dd）！", e);
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取图片信息失败，参数非法（要求格式yyyy-MM-dd）！");
				return resp;
			}
			if (epgType == 0){
				epgType = EpgType.STANDARD.getType();
			}
			TSigninPicture tmpTSigninPicture = entityDao.get(TSigninPicture.class, "o.tsignin.code=?1 and o.aboveTime=?2 and epgType=?3", 
					new Object[]{signinCode, aboveTime, epgType});
			if (tmpTSigninPicture == null){
				logger.error("图片不存在！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("图片不存在！");
				return resp;
			}
			Integer tmpStatus = tmpTSigninPicture.getStatus();
			if (tmpStatus == 0){
				logger.error("图片已经停用！");
				resp.setResultCode(LuckySigninResultCode.PICTURE_HAS_BEEN_STOPPED);
				resp.setResultDesc("图片已经停用！");
				return resp;
			}
			resp.setAboveTime(tmpTSigninPicture.getAboveTime());
			resp.setCode(tmpTSigninPicture.getCode());
			resp.setCpCode(tmpTSigninPicture.getCpCode());
			resp.setName(tmpTSigninPicture.getName());
			if (!StringUtils.isEmpty(tmpTSigninPicture.getPicture())){
				resp.setPicture(getHttpUrl(tmpTSigninPicture.getPicture()));
			}
			resp.setProfile(tmpTSigninPicture.getProfile());
			logger.info("获取图片信息成功！");
		} catch (Exception e) {
			logger.error("获取图片信息失败，程序异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取图片信息失败，程序异常！");
		}
		return resp;
		
	}

	public GetSigninPrizeListResp getSigninPrizeList(String signinCode) {
		
		GetSigninPrizeListResp resp = new GetSigninPrizeListResp();
		if (StringUtils.isBlank(signinCode)){
			logger.error("签到编码非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("签到编码非法！");
			return resp;
		}
		try {
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("sequence", "desc");
			List<TSigninPrize> tmpListTSigninPrize = entityDao.getScrollData(TSigninPrize.class, -1, -1, "o.tsignin.code=?1 and o.status=1", new Object[]{signinCode}, orderBy);
			if (tmpListTSigninPrize == null || tmpListTSigninPrize.isEmpty()) return resp;
			List<GetSigninPrizeListResp.SigninPrizeInfo> tmpListSigninPrizeInfo = new ArrayList<GetSigninPrizeListResp.SigninPrizeInfo>();
			GetSigninPrizeListResp.SigninPrizeInfo tmpSigninPrizeInfo;
			
			for (TSigninPrize tmpTSigninPrize : tmpListTSigninPrize){
				tmpSigninPrizeInfo = new GetSigninPrizeListResp.SigninPrizeInfo();
				tmpSigninPrizeInfo.setConsumeGold(tmpTSigninPrize.getConsumeGold());
				tmpSigninPrizeInfo.setExchangeNum(tmpTSigninPrize.getExchangeNum());
				tmpSigninPrizeInfo.setLimitNum(tmpTSigninPrize.getLimitNum());
				tmpSigninPrizeInfo.setNumber(tmpTSigninPrize.getNumber());
				tmpSigninPrizeInfo.setPrizeCode(tmpTSigninPrize.getCode());
				tmpSigninPrizeInfo.setPrizeName(tmpTSigninPrize.getName());
				if (!StringUtils.isEmpty(tmpTSigninPrize.getPicture())){
					tmpSigninPrizeInfo.setPrizePicture(getHttpUrl(tmpTSigninPrize.getPicture()));
				}
				tmpSigninPrizeInfo.setPrizePrice(tmpTSigninPrize.getPrice());
				tmpSigninPrizeInfo.setPrizeType(tmpTSigninPrize.getType());
				tmpSigninPrizeInfo.setSequence(tmpTSigninPrize.getSequence());
				tmpListSigninPrizeInfo.add(tmpSigninPrizeInfo);
			}
			resp.setSigninPrizeInfos(tmpListSigninPrizeInfo);
			resp.setRecordSize(tmpListSigninPrizeInfo.size());
			logger.info("获取奖品列表成功");
		} catch (Exception e) {
			logger.error("获取奖品列表失败，程序异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取奖品列表失败，程序异常！");
		}
		return resp;
	}

	public GetSigninPrizeDetailResp getSigninPrizeDetail(String prizeCode) {
		
		GetSigninPrizeDetailResp resp = new GetSigninPrizeDetailResp();
		if (StringUtils.isBlank(prizeCode)){
			logger.error("奖品编码非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("奖品编码非法！");
			return resp;
		}
		try {
			TSigninPrize tmpTSigninPrize = entityDao.get(TSigninPrize.class, "o.code=?1", new Object[]{prizeCode});
			if (tmpTSigninPrize == null){
				logger.error("奖品不存在，奖品编码非法！");
				resp.setResultCode(LuckySigninResultCode.PRIZE_DOES_NOT_EXIST);
				resp.setResultDesc("奖品不存在，奖品编码非法！");
				return resp;
			}
			Integer tmpStatus = tmpTSigninPrize.getStatus();
			if (tmpStatus == 0){
				logger.error("奖品已经停用！");
				resp.setResultCode(LuckySigninResultCode.PRIZE_HAS_BEEN_STOPPED);
				resp.setResultDesc("奖品已经停用！");
				return resp;
			}
			resp.setConsumeGold(tmpTSigninPrize.getConsumeGold());
			resp.setExchangeNum(tmpTSigninPrize.getExchangeNum());
			resp.setLimitNum(tmpTSigninPrize.getLimitNum());
			resp.setNumber(tmpTSigninPrize.getNumber());
			resp.setPrizeCode(tmpTSigninPrize.getCode());
			resp.setPrizeName(tmpTSigninPrize.getName());
			if (!StringUtils.isEmpty(tmpTSigninPrize.getPicture())){
				resp.setPrizePicture(getHttpUrl(tmpTSigninPrize.getPicture()));
			}
			resp.setPrizePrice(tmpTSigninPrize.getPrice());
			resp.setPrizeType(tmpTSigninPrize.getType());
			resp.setSequence(tmpTSigninPrize.getSequence());
			logger.info("获取奖品信息成功！");
		} catch (Exception e) {
			logger.error("获取奖品信息失败，系统异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取奖品信息失败，系统异常！");
		}
		return resp;
	}

	public GetSigninProductDetailResp getSigninProductDetail(String signinCode, String productID) {
		
		GetSigninProductDetailResp resp = new GetSigninProductDetailResp();
		if (StringUtils.isBlank(signinCode)){
			logger.error("签到编码非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("签到编码非法！");
			return resp;
		}
		if (StringUtils.isBlank(productID)){
			logger.error("产品编码非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("产品编码非法！");
			return resp;
		}
		try {
			TSigninProduct tmpTSigninProduct = entityDao.get(TSigninProduct.class, "o.tsignin.code=?1 and o.productCode=?2", new Object[]{signinCode, productID});
			if (tmpTSigninProduct == null){
				logger.error("产品不存在，奖品编码非法！");
				resp.setResultCode(LuckySigninResultCode.PRODUCT_DOES_NOT_EXIST);
				resp.setResultDesc("产品不存在，奖品编码非法！");
				return resp;
			}
			Integer tmpStatus = tmpTSigninProduct.getStatus();
			if (tmpStatus == 0){
				logger.error("产品已经停用！");
				resp.setResultCode(LuckySigninResultCode.PRODUCT_HAS_BEEN_STOPPED);
				resp.setResultDesc("产品已经停用！");
				return resp;
			}
			resp.setOrderGoldNum(tmpTSigninProduct.getOrderGoldNum());
			resp.setSigninGoldNum(tmpTSigninProduct.getSigninGoldNum());
			resp.setProductCode(tmpTSigninProduct.getProductCode());
			resp.setProductName(tmpTSigninProduct.getProductName());
			resp.setSequence(tmpTSigninProduct.getSequence());
			logger.info("获取产品信息成功！");
		} catch (Exception e) {
			logger.error("获取产品信息失败，系统异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取产品信息失败，系统异常！");
		}
		return resp;
	}

	public GetSigninProductListResp getSigninProductList(String signinCode) {
		
		GetSigninProductListResp resp = new GetSigninProductListResp();
		if (StringUtils.isBlank(signinCode)){
			logger.error("签到编码非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("签到编码非法！");
			return resp;
		}
		try {
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("sequence", "desc");
			List<TSigninProduct> tmpListTSigninProduct = entityDao.getScrollData(TSigninProduct.class, -1, -1, "o.tsignin.code=?1 and o.status=1", new Object[]{signinCode}, orderBy);
			if (tmpListTSigninProduct == null || tmpListTSigninProduct.isEmpty()) return resp;
			List<GetSigninProductListResp.SigninProductInfo> tmpListSigninProductInfo = new ArrayList<GetSigninProductListResp.SigninProductInfo>();
			GetSigninProductListResp.SigninProductInfo tmpSigninProductInfo;
			
			for (TSigninProduct tmpTSigninProduct : tmpListTSigninProduct){
				tmpSigninProductInfo = new GetSigninProductListResp.SigninProductInfo();
				tmpSigninProductInfo.setOrderGoldNum(tmpTSigninProduct.getOrderGoldNum());
				tmpSigninProductInfo.setSigninGoldNum(tmpTSigninProduct.getSigninGoldNum());
				tmpSigninProductInfo.setProductCode(tmpTSigninProduct.getProductCode());
				tmpSigninProductInfo.setProductName(tmpTSigninProduct.getProductName());
				tmpSigninProductInfo.setSequence(tmpTSigninProduct.getSequence());
				
				tmpListSigninProductInfo.add(tmpSigninProductInfo);
			}
			resp.setSigninProductInfos(tmpListSigninProductInfo);
			resp.setRecordSize(tmpListSigninProductInfo.size());
			logger.info("获取产品列表成功");
		} catch (Exception e) {
			logger.error("获取产品列表失败，程序异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取产品列表失败，程序异常！");
		}
		return resp;
	}

	public LuckySigninResp getSigninUser(String signinCode, String userID) {
		
		LuckySigninResp resp = new LuckySigninResp();
		if (StringUtils.isBlank(signinCode)){
			logger.error("签到编码非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("签到编码非法！");
			return resp;
		}
		if (StringUtils.isBlank(userID)){
			logger.error("账号非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("账号非法！");
			return resp;
		}
		try {
			TSigninUser tmpTSigninUser = entityDao.get(TSigninUser.class, "o.tsignin.code=?1 and o.userId=?2", new Object[]{signinCode, userID});
			if (tmpTSigninUser == null){
				logger.error("签到用户不存在，账号非法！");
				resp.setResultCode(LuckySigninResultCode.USER_DOES_NOT_EXIST);
				resp.setResultDesc("签到用户不存在，账号非法！");
				return resp;
			}
			Integer tmpStatus = tmpTSigninUser.getStatus();
			if (tmpStatus == 0){
				logger.error("签到用户已经停用！");
				resp.setResultCode(LuckySigninResultCode.USER_HAS_BEEN_STOPPED);
				resp.setResultDesc("签到用户已经停用！");
				return resp;
			}
			resp.setUserID(userID);
			resp.setGoldNum(tmpTSigninUser.getGoldNum() == null ? 0 : tmpTSigninUser.getGoldNum());
			logger.info("获取签到用户信息成功！");
		} catch (Exception e) {
			logger.error("获取签到用户信息失败，系统异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取签到用户信息失败，系统异常！");
		}
		return resp;
	}

	public GetSigninExchangeListResp getSigninExchangeList(String signinCode,
                                                           String userID, String prizeCode, int firstIndex, int maxResult) {
		
		GetSigninExchangeListResp resp = new GetSigninExchangeListResp();
		try {
			if (StringUtils.isBlank(signinCode)){
				logger.error("签到编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("签到编码非法！");
				return resp;
			}
			String tmpWhereHql = "o.tsignin.code=?";
			String tmpWhereJpql = "o.tsignin.code=?1";
			int tmpParamIndex = 1;
			List<String> tmpListParams = new ArrayList<String>();
			tmpListParams.add(signinCode);
			if (!StringUtils.isBlank(userID)){
				tmpParamIndex = tmpParamIndex + 1;
				tmpWhereHql = tmpWhereHql + " and o.userId=?";
				tmpWhereJpql = tmpWhereJpql + " and o.userId=?" + tmpParamIndex;
				tmpListParams.add(userID);
			}
			if (!StringUtils.isBlank(prizeCode)){
				tmpParamIndex = tmpParamIndex + 1;
				tmpWhereHql = tmpWhereHql + " and o.prizeCode=?";
				tmpWhereJpql = tmpWhereJpql + " and o.prizeCode=?" + tmpParamIndex;
				tmpListParams.add(prizeCode);
			}
			Object[] tmpQueryParams = tmpListParams.toArray();
			
			String tmpCountHql = "select count(o.id) as count from TSigninExchange o where " + tmpWhereHql;
			List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpCountHql, tmpQueryParams, -1, -1);
			int tmpIntCount = 0;
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				Long tmpLongCount = (Long) tmpMapCount.get("count");
				tmpIntCount = tmpLongCount.intValue();
			}
			if(tmpIntCount <= 0) return resp;
			 
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("exchangeTime", "desc");
			List<TSigninExchange> tmpListTSigninExchange = entityDao.getScrollData(TSigninExchange.class, firstIndex, maxResult, tmpWhereJpql, tmpQueryParams, orderBy);
			if (tmpListTSigninExchange == null || tmpListTSigninExchange.isEmpty()) return resp;
			List<GetSigninExchangeListResp.SigninExchangeInfo> tmpListSigninExchangeInfo = new ArrayList<GetSigninExchangeListResp.SigninExchangeInfo>();
			GetSigninExchangeListResp.SigninExchangeInfo tmpSigninExchangeInfo;
			for (TSigninExchange tmpTSigninExchange : tmpListTSigninExchange){
				tmpSigninExchangeInfo = new GetSigninExchangeListResp.SigninExchangeInfo();
				tmpSigninExchangeInfo.setExchangeGold(tmpTSigninExchange.getExchangeGold());
				tmpSigninExchangeInfo.setExchangeTime(DateFormatUtil.format(tmpTSigninExchange.getExchangeTime(),"yyyyMMddHHmmss"));
				tmpSigninExchangeInfo.setMobile(tmpTSigninExchange.getMobile());
				tmpSigninExchangeInfo.setPrizeCode(tmpTSigninExchange.getPrizeCode());
				tmpSigninExchangeInfo.setPrizeName(tmpTSigninExchange.getPrizeName());
				tmpSigninExchangeInfo.setUserID(tmpTSigninExchange.getUserId());
				
				tmpListSigninExchangeInfo.add(tmpSigninExchangeInfo);
			}
			resp.setSigninExchangeInfos(tmpListSigninExchangeInfo);
			resp.setRecordSize(tmpIntCount);
			logger.info("获取签到兑换列表成功！");
		} catch (Exception e) {
			logger.error("获取签到兑换列表失败，程序异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取签到兑换列表失败，程序异常！");
		}
		return resp;
	}

	@Transactional
	public LuckySigninResp initSigninUser(String signinCode, String userID,
			String productID) {
		LuckySigninResp resp = new LuckySigninResp();
		resp.setGoldNum(0);
		Date currentDate = new Date();
		
		try {
			if (StringUtils.isBlank(signinCode)){
				logger.error("初始化签到用户失败，签到编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("初始化签到用户失败，签到编码非法！");
				return resp;
			}
			if (StringUtils.isEmpty(userID)){
				logger.error("初始化签到用户失败，账号非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("初始化签到用户失败，账号非法！");
				return resp;
			}
			TSignin tmpTSignin = entityDao.get(TSignin.class, "o.code=?1", new Object[]{signinCode});
			if (tmpTSignin == null){
				logger.error("初始化签到用户失败，签到编码不存在！");
				resp.setResultCode(LuckySigninResultCode.SIGNIN_DOES_NOT_EXIST);
				resp.setResultDesc("初始化签到用户失败，签到编码不存在！");
				return resp;
			}
			//验证签到活动有效性
			ValidateRets<TSignin>  tmpTSigninValidateRets = signinValidate.validate(new SigninValidateArgs(resp, 
					tmpTSignin, currentDate));
			if (!tmpTSigninValidateRets.isSuccess()) return resp;
			
			TSigninUser tmpTSigninUser = entityDao.get(TSigninUser.class, "o.userId=?1", new Object[]{userID});
			
			if (StringUtils.isBlank(productID)){
				if (tmpTSigninUser == null){
					tmpTSigninUser = insertSigninUser(tmpTSignin, userID, 0, null, null, currentDate);
					logger.info("初始化签到用户成功！");
					resp.setResultDesc("初始化签到用户成功！");
				}else{
					logger.info("初始化签到用户成功，用户已存在！");
					resp.setResultDesc("初始化签到用户成功，用户已存在！");
				}
				resp.setGoldNum(tmpTSigninUser.getGoldNum());
				resp.setUserID(userID);
				resp.setMobile(tmpTSigninUser.getMobile());
				return resp;
			}
			
			String[] tmpArrayProductID = productID.split(",");
			TSigninProduct tmpTSigninProduct;
			String tmpProductID;
			String tmpProductGolds = "";
			String tmpProductCodes = "";
			int tmpTotalGoldNum = 0;
			Map<String, Integer> tmpDestUserGoldMap = new LinkedHashMap<String, Integer>();
			for (int i=0;i<tmpArrayProductID.length; i++){
				tmpProductID = tmpArrayProductID[i];
				tmpTSigninProduct = entityDao.get(TSigninProduct.class, "o.productCode=?1 and o.tsignin.code=?2 and status=1", 
						new Object[]{tmpProductID, signinCode});
				if (tmpTSigninProduct == null) continue;
				int tmpGoldNum = tmpTSigninProduct.getOrderGoldNum() == null ? 0 : tmpTSigninProduct.getOrderGoldNum();
				if (tmpGoldNum < 0) tmpGoldNum = 0;
				
				tmpDestUserGoldMap.put(tmpArrayProductID[i], tmpGoldNum);
				tmpTotalGoldNum = tmpTotalGoldNum + tmpGoldNum;
				tmpProductCodes = tmpProductCodes + tmpProductID + ",";
				tmpProductGolds = tmpProductGolds + tmpGoldNum + ",";
			}
			if (StringUtils.isBlank(tmpProductCodes)){
				if (tmpTSigninUser == null){
					tmpTSigninUser = insertSigninUser(tmpTSignin, userID, 0, null, null, currentDate);
					logger.info("初始化签到用户成功！");
					resp.setResultDesc("初始化签到用户成功！");
				}else{
					logger.info("初始化签到用户成功，用户已存在！");
					resp.setResultDesc("初始化签到用户成功，用户已存在！");
				}
				resp.setGoldNum(tmpTSigninUser.getGoldNum());
				resp.setUserID(userID);
				resp.setMobile(tmpTSigninUser.getMobile());
				return resp;
			}
			
			tmpProductCodes = StringUtils.removeEnd(tmpProductCodes, ",");
			tmpProductGolds = StringUtils.removeEnd(tmpProductGolds, ",");
			
			if (tmpTSigninUser == null){
				tmpTSigninUser = insertSigninUser(tmpTSignin, userID, tmpTotalGoldNum, tmpProductCodes, tmpProductGolds, currentDate);
				logger.info("初始化签到用户成功！");
				resp.setResultDesc("初始化签到用户成功！");
				
				resp.setGoldNum(tmpTSigninUser.getGoldNum());
				resp.setUserID(userID);
				return resp;
			}
			
			String tmpSrcProductCodes = tmpTSigninUser.getProductCodes();
			String tmpSrcProductGolds = tmpTSigninUser.getProductGolds();
			int tmpSrcGoldNum = tmpTSigninUser.getGoldNum() == null ? 0 : tmpTSigninUser.getGoldNum();
			if (tmpSrcGoldNum < 0) tmpSrcGoldNum = 0;
			if (StringUtils.isBlank(tmpSrcProductCodes) || StringUtils.isBlank(tmpSrcProductGolds)){
				tmpTSigninUser.setProductCodes(tmpProductCodes);
				tmpTSigninUser.setProductGolds(tmpProductGolds);
				tmpTSigninUser.setGoldNum(tmpTotalGoldNum + tmpSrcGoldNum);
				tmpTSigninUser.setUpdateTime(currentDate);
				entityDao.update(tmpTSigninUser);
				logger.info("初始化签到用户成功，更新用户的订购产品！");
				resp.setResultDesc("初始化签到用户成功，更新用户的订购产品！");
				
				resp.setGoldNum(tmpTSigninUser.getGoldNum());
				resp.setUserID(userID);
				resp.setMobile(tmpTSigninUser.getMobile());
				return resp;
			}
			if (tmpSrcProductCodes.equals(tmpProductCodes) && tmpSrcProductGolds.equals(tmpProductGolds)){
				logger.info("初始化签到用户成功，用户已存在！");
				resp.setResultDesc("初始化签到用户成功，用户已存在！");
				
				resp.setGoldNum(tmpTSigninUser.getGoldNum());
				resp.setUserID(userID);
				resp.setMobile(tmpTSigninUser.getMobile());
				return resp;
			}
			String[] tmpSrcArrayProductID = tmpProductCodes.split(",");
			String[] tmpSrcArrayProductGolds = tmpSrcProductGolds.split(",");
			if (tmpSrcArrayProductID.length != tmpSrcArrayProductGolds.length){
				logger.error("初始化签到用户失败，用户数据错误！");
				resp.setResultDesc("初始化签到用户失败，用户数据错误");
				resp.setResultCode(LuckySigninResultCode.DATABASE_EXCEPTION);
				return resp;
			}
			
			/**
			 * 新增订购产品则增加签到用户的金币数量
			 */
			Map<String, Integer> tmpSrcUserGoldMap = new LinkedHashMap<String, Integer>();
			for (int i=0; i<tmpSrcArrayProductID.length;i++ ){
				tmpSrcUserGoldMap.put(tmpSrcArrayProductID[i], NumberUtils.toInt(tmpSrcArrayProductGolds[i]));
			}
			Set<String> tmpDestKeySet = tmpDestUserGoldMap.keySet();
			Integer tmpSrcValue;
			Integer tmpDestValue;
			int tmpAddGold = 0;
			for (String tmpDestKey : tmpDestKeySet){
				tmpSrcValue = tmpSrcUserGoldMap.get(tmpDestKey);
				tmpDestValue = tmpDestUserGoldMap.get(tmpDestKey);
				if (tmpSrcValue == null){
					tmpAddGold = tmpAddGold + tmpDestValue;
				}
			}
			int tmpGoldNum = tmpTSigninUser.getGoldNum() == null ? 0 : tmpTSigninUser.getGoldNum();
			if (tmpGoldNum < 0) tmpGoldNum = 0;
			//订购金币不变，不做更新
			if (tmpAddGold > 0){
				tmpGoldNum = tmpGoldNum + tmpAddGold;
				tmpTSigninUser.setGoldNum(tmpGoldNum);
				tmpTSigninUser.setUpdateTime(currentDate);
			}
			
			logger.info("初始化签到用户成功，用户已存在！");
			resp.setResultDesc("初始化签到用户成功，用户已存在！");
			
			resp.setGoldNum(tmpTSigninUser.getGoldNum());
			resp.setUserID(userID);
			resp.setMobile(tmpTSigninUser.getMobile());
			return resp;
		} catch (Exception e) {
			logger.error("初始化签到用户失败，程序异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("初始化签到用户失败，程序异常！");
		}
		return resp;
	}
	
	private TSigninUser insertSigninUser(TSignin tsignin, String userID, int goldNum,
			String productCodes, String productGolds, Date currentDate){
		TSigninUser tmpTSigninUser = new TSigninUser();
		tmpTSigninUser.setCpCode(tsignin.getCpCode());
		tmpTSigninUser.setCreateTime(currentDate);
		tmpTSigninUser.setGoldNum(goldNum);
		tmpTSigninUser.setStatus(1);
		tmpTSigninUser.setUserId(userID);
		tmpTSigninUser.setTsignin(tsignin);
		tmpTSigninUser.setProductCodes(productCodes);
		tmpTSigninUser.setProductGolds(productGolds);
		
		entityDao.saveObj(tmpTSigninUser);
		return tmpTSigninUser;
	}
	
	
	@Transactional
	public LuckySigninResp luckySignin(String signinCode, String userID, String productID, String signinTime, String randomStr) {
		LuckySigninResp resp = new LuckySigninResp();
		resp.setGoldNum(0);
		try {
			if (StringUtils.isBlank(signinCode)){
				logger.error("签到失败，签到编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("签到失败，签到编码非法！");
				return resp;
			}
			if (StringUtils.isBlank(userID)){
				logger.error("签到失败，账号非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("签到失败，账号非法！");
				return resp;
			}
			
			Date currentDate = new Date();
			
			if (!StringUtils.isBlank(signinTime)){
				try{
					currentDate = DateUtils.parseDate(signinTime, new String[]{"yyyyMMddHHmmss"});
				} catch (ParseException e){
					logger.error("签到失败，签到时间非法（要求格式yyyyMMddHHmmss）！");
					resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
					resp.setResultDesc("签到失败，签到时间非法（要求格式yyyyMMddHHmmss）！");
					return resp;
				}
			}
			TSignin tmpTSignin = entityDao.get(TSignin.class, "o.code=?1", new Object[]{signinCode});
			if (tmpTSignin == null){
				logger.error("签到失败，签到编码不存在！");
				resp.setResultCode(LuckySigninResultCode.SIGNIN_DOES_NOT_EXIST);
				resp.setResultDesc("签到失败，签到编码不存在！");
				return resp;
			}
			
			//验证签到活动有效性
			ValidateRets<TSignin> tmpTSigninValidateRets = signinValidate.validate(new SigninValidateArgs(resp, 
					tmpTSignin, currentDate));
			if (!tmpTSigninValidateRets.isSuccess()) return resp;
			
			TSigninUser tmpTSigninUser = entityDao.get(TSigninUser.class, "o.tsignin.code=?1 and o.userId=?2", 
					new Object[]{signinCode, userID});
			if (tmpTSigninUser == null){
				logger.error("签到失败，签到用户不存在！");
				resp.setResultCode(LuckySigninResultCode.USER_DOES_NOT_EXIST);
				resp.setResultDesc("签到失败，签到用户不存在！");
				return resp;
			}
			if (tmpTSigninUser.getStatus() == 0){
				logger.error("签到失败，签到用户已停用！");
				resp.setResultCode(LuckySigninResultCode.USER_HAS_BEEN_STOPPED);
				resp.setResultDesc("签到失败，签到用户已停用！");
				return resp;
			}
			
			TSigninRecordDay tmpTSigninRecordDay;
			if (StringUtils.isBlank(productID)){
				tmpTSigninRecordDay = entityDao.get(TSigninRecordDay.class, "o.tsignin.code=?1 and o.userId=?2 and date_format(o.signinTime,'%Y%m%d')=?3", 
						new Object[]{signinCode, userID, DateFormatUtil.format(currentDate, "yyyyMMdd")});
				if (tmpTSigninRecordDay != null){
					logger.error("签到失败，用户当天已签到！");
					resp.setResultCode(LuckySigninResultCode.HAS_BEEN_SIGNIN);
					resp.setResultDesc("签到失败，用户当天已签到！");
					return resp;
				}
				int tmpSigninGoldNum = tmpTSignin.getGoldNum() == null ? 0 : tmpTSignin.getGoldNum();
				if (tmpSigninGoldNum < 0) tmpSigninGoldNum = 0;
				int tmpUserGoldNum = tmpTSigninUser.getGoldNum() == null ? 0 : tmpTSigninUser.getGoldNum();
				if (tmpUserGoldNum < 0) tmpUserGoldNum = 0;
				tmpUserGoldNum = tmpUserGoldNum + tmpSigninGoldNum;
				
				//新增签到记录
				insertSigninRecord(tmpTSignin, null, userID, tmpSigninGoldNum, randomStr, currentDate);
				
				if (tmpSigninGoldNum > 0){
					//修改用户的签到金牌数量
					tmpTSigninUser.setGoldNum(tmpUserGoldNum);
					tmpTSigninUser.setUpdateTime(new Date());
					entityDao.update(tmpTSigninUser);
				}
				resp.setGoldNum(tmpTSigninUser.getGoldNum());
				logger.info("签到成功！");
				return resp;
			}
			
			TSigninProduct tmpTSigninProduct = entityDao.get(TSigninProduct.class, "o.tsignin.code=?1 and o.productCode=?2", new Object[]{signinCode, productID});
			if (tmpTSigninProduct == null){
				logger.error("签到失败，签到产品不存在！");
				resp.setGoldNum(tmpTSigninUser.getGoldNum());
				resp.setResultCode(LuckySigninResultCode.PRODUCT_DOES_NOT_EXIST);
				resp.setResultDesc("签到失败，签到产品不存在！");
				return resp;
			}
			if (tmpTSigninProduct.getStatus() == 0){
				logger.error("签到失败，签到产品已停用！");
				resp.setGoldNum(tmpTSigninUser.getGoldNum());
				resp.setResultCode(LuckySigninResultCode.PRODUCT_HAS_BEEN_STOPPED);
				resp.setResultDesc("签到失败，签到产品已停用！");
				return resp;
			}
		
			tmpTSigninRecordDay = entityDao.get(TSigninRecordDay.class, "o.tsignin.code=?1 and o.userId=?2 and date_format(o.signinTime,'%Y%m%d')=?3 and o.productCode=?4", 
					new Object[]{signinCode, userID, DateFormatUtil.format(currentDate, "yyyyMMdd"), productID});
			if (tmpTSigninRecordDay != null){
				logger.error(String.format("签到失败，产品[%s]用户[%s]当天已签到！", productID, userID));
				resp.setGoldNum(tmpTSigninUser.getGoldNum());
				resp.setResultCode(LuckySigninResultCode.HAS_BEEN_SIGNIN);
				resp.setResultDesc(String.format("签到失败，产品[%s]用户[%s]当天已签到！", productID, userID));
				return resp;
			}
			
			int tmpSigninGoldNum = tmpTSigninProduct.getSigninGoldNum() == null ? 0 : tmpTSigninProduct.getSigninGoldNum();
			if (tmpSigninGoldNum < 0) tmpSigninGoldNum = 0;
			int tmpUserGoldNum = tmpTSigninUser.getGoldNum() == null ? 0 : tmpTSigninUser.getGoldNum();
			if (tmpUserGoldNum < 0) tmpUserGoldNum = 0;
			tmpUserGoldNum = tmpUserGoldNum + tmpSigninGoldNum;
			//新增签到记录
			insertSigninRecord(tmpTSignin, tmpTSigninProduct, userID, tmpSigninGoldNum, randomStr, currentDate);
			
			if (tmpSigninGoldNum > 0){
				//修改用户的签到金牌数量
				tmpTSigninUser.setGoldNum(tmpUserGoldNum);
				tmpTSigninUser.setUpdateTime(new Date());
				entityDao.update(tmpTSigninUser);
			}
			
			logger.info("签到成功！");
			resp.setGoldNum(tmpTSigninUser.getGoldNum());
		} catch (Exception e) {
			logger.error("签到失败，程序异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("签到失败，程序异常！");
		}
		return resp;
		
	}
	
	private void insertSigninRecord(TSignin tsignin, TSigninProduct tsigninProduct, String userID, 
			int signinGoldNum, String randomStr, Date currentDate){
		TSigninRecord tmpTSigninRecord = new TSigninRecord();
		tmpTSigninRecord.setCpCode(tsignin.getCpCode());
		tmpTSigninRecord.setGoldNum(signinGoldNum);
		tmpTSigninRecord.setRandomStr(randomStr);
		tmpTSigninRecord.setSigninTime(currentDate);
		tmpTSigninRecord.setTsignin(tsignin);
		tmpTSigninRecord.setUserId(userID);
		if (tsigninProduct != null){
			tmpTSigninRecord.setProductCode(tsigninProduct.getProductCode());
			tmpTSigninRecord.setProductName(tsigninProduct.getProductName());
		}
		entityDao.saveObj(tmpTSigninRecord);
		
		TSigninRecordDay tmpTSigninRecordDay = new TSigninRecordDay();
		tmpTSigninRecordDay.setCpCode(tsignin.getCpCode());
		tmpTSigninRecordDay.setGoldNum(signinGoldNum);
		tmpTSigninRecordDay.setRandomStr(randomStr);
		tmpTSigninRecordDay.setSigninTime(currentDate);
		tmpTSigninRecordDay.setTsignin(tsignin);
		tmpTSigninRecordDay.setUserId(userID);
		if (tsigninProduct != null){
			tmpTSigninRecordDay.setProductCode(tsigninProduct.getProductCode());
			tmpTSigninRecordDay.setProductName(tsigninProduct.getProductName());
		}
		entityDao.saveObj(tmpTSigninRecordDay);
	}

	@Transactional
	public LuckySigninResp exchangePrize(String prizeCode, String userID) {
		LuckySigninResp resp = new LuckySigninResp();
		resp.setGoldNum(0);
		try {
			if (StringUtils.isBlank(prizeCode)){
				logger.error("兑换失败，奖品编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("兑换失败，奖品编码非法！");
				return resp;
			}
			if (StringUtils.isEmpty(userID)){
				logger.error("兑换失败，账号非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("兑换失败，账号非法！");
				return resp;
			}
			
			TSigninPrize tmpTSigninPrize = entityDao.get(TSigninPrize.class, "o.code=?1", new Object[]{prizeCode});
			if (tmpTSigninPrize == null){
				logger.error("兑换失败，奖品编码不存在！");
				resp.setResultCode(LuckySigninResultCode.PRIZE_DOES_NOT_EXIST);
				resp.setResultDesc("兑换失败，奖品编码不存在！");
				return resp;
			}
			if (tmpTSigninPrize.getStatus() == 0){
				logger.error("兑换失败，奖品已停用！");
				resp.setResultCode(LuckySigninResultCode.PRIZE_HAS_BEEN_STOPPED);
				resp.setResultDesc("兑换失败，奖品已停用！");
				return resp;
			}
			TSignin tmpTSignin = tmpTSigninPrize.getTsignin();
			if (tmpTSignin == null){
				logger.error("兑换失败，签到活动不存在！");
				resp.setResultCode(LuckySigninResultCode.SIGNIN_DOES_NOT_EXIST);
				resp.setResultDesc("兑换失败，签到活动不存在！！");
				return resp;
			}
			
			Date currentDate = new Date();
			//验证签到活动有效性
			ValidateRets<TSignin> tmpTSigninValidateRets = signinValidate.validate(new SigninValidateArgs(resp, 
					tmpTSignin, currentDate));
			if (!tmpTSigninValidateRets.isSuccess()) return resp;
			
			TSigninUser tmpTSigninUser = entityDao.get(TSigninUser.class, "o.tsignin.code=?1 and o.userId=?2", 
					new Object[]{tmpTSignin.getCode(), userID});
			if (tmpTSigninUser == null){
				logger.error("兑换失败，签到用户不存在！");
				resp.setResultCode(LuckySigninResultCode.USER_DOES_NOT_EXIST);
				resp.setResultDesc("兑换失败，签到用户不存在！");
				return resp;
			}
			if (tmpTSigninUser.getStatus() == 0){
				logger.error("兑换失败，签到用户已停用！");
				resp.setResultCode(LuckySigninResultCode.USER_HAS_BEEN_STOPPED);
				resp.setResultDesc("兑换失败，签到用户已停用！");
				return resp;
			}
			resp.setMobile(tmpTSigninUser.getMobile());
			resp.setUserID(tmpTSigninUser.getUserId());
			
			int tmpLimitNum = tmpTSigninPrize.getLimitNum() == null ? 0 : tmpTSigninPrize.getLimitNum();
			if (tmpLimitNum < 0) tmpLimitNum = 0;
			int tmpPrizeNum = tmpTSigninPrize.getNumber() == null ? 0 : tmpTSigninPrize.getNumber();
			if (tmpPrizeNum < 0) tmpPrizeNum = 0;
			int tmpExchangeNum = tmpTSigninPrize.getExchangeNum() == null ? 0 : tmpTSigninPrize.getExchangeNum();
			if (tmpExchangeNum < 0) tmpExchangeNum = 0;
			int tmpConsumeGold = tmpTSigninPrize.getConsumeGold() == null ? 0 : tmpTSigninPrize.getConsumeGold();
			if (tmpConsumeGold < 0) tmpConsumeGold = 0;
			int tmpUserGoldNum = tmpTSigninUser.getGoldNum() == null ? 0 : tmpTSigninUser.getGoldNum();
			if (tmpUserGoldNum < 0) tmpUserGoldNum = 0;
			
			//限制数量
			if (tmpLimitNum != 0){
				if (tmpPrizeNum == 0){
					logger.error("兑换失败，奖品数量不足！");
					resp.setResultCode(LuckySigninResultCode.PRIZE_NUM_NOT_ENOUGH);
					resp.setResultDesc("兑换失败，奖品数量不足！");
					resp.setGoldNum(tmpTSigninUser.getGoldNum());
					return resp;
				}
				//奖品数量减1
				tmpPrizeNum = tmpPrizeNum - 1;
			}
			
			int tmpPrizeType = tmpTSigninPrize.getType() == null ? PrizeType.OHTER.getType() : tmpTSigninPrize.getType();
			//奖品类型为充值卡，需要获取卡号，卡密
			TCardDetail tmpTCardDetail = null;
			if (tmpPrizeType == PrizeType.PREPAID_PHONE_CARD.getType()){
				Long tmpCardId = tmpTSigninPrize.getCardId();
				if (tmpCardId == null){
					logger.error("兑换失败，奖品绑定的关联制卡ID为空！");
					resp.setResultCode(LuckySigninResultCode.DATABASE_EXCEPTION);
					resp.setResultDesc("兑换失败，奖品绑定的关联制卡ID为空！");
					resp.setGoldNum(tmpTSigninUser.getGoldNum());
					return resp;
				}
				String tmpStrCurrentDate = DateFormatUtil.format(currentDate, "yyyy-MM-dd");
				tmpTCardDetail = entityDao.get(TCardDetail.class, 
						 "o.tcardInfo.id=?1 and o.status=?2 and o.expireDate>='" + tmpStrCurrentDate + "'", 
						 new Object[]{tmpCardId, CardDetailStatus.NOT_USED.getStatus()});
				 if (tmpTCardDetail == null){
					logger.error("兑换失败，奖品数量不足！");
					resp.setResultCode(LuckySigninResultCode.PRIZE_NUM_NOT_ENOUGH);
					resp.setResultDesc("兑换失败，奖品数量不足！");
					resp.setGoldNum(tmpTSigninUser.getGoldNum());
					return resp;
				 }
			}
			
			if (tmpConsumeGold > 0){
				tmpUserGoldNum = tmpUserGoldNum - tmpConsumeGold;
				if (tmpUserGoldNum < 0){
					logger.error("兑换失败，用户金币不足！");
					resp.setResultCode(LuckySigninResultCode.USER_GOLD_NOT_ENOUGH);
					resp.setResultDesc("兑换失败，用户金币不足！");
					resp.setGoldNum(tmpTSigninUser.getGoldNum());
					return resp;
				}
				//更新用户金币
				tmpTSigninUser.setUpdateTime(currentDate);
				tmpTSigninUser.setGoldNum(tmpUserGoldNum);
				entityDao.update(tmpTSigninUser);
			}
			
			//新增兑换记录
			TSigninExchange tmpTSigninExchange = new TSigninExchange();
			
			if (tmpTCardDetail != null && tmpPrizeType == PrizeType.PREPAID_PHONE_CARD.getType()){
				/**
				 * 绑定账户
				 */
				tmpTCardDetail.setAccount(userID);
				/**
				  * 更新卡状态为已使用
				  */
				tmpTCardDetail.setStatus(CardDetailStatus.BEEN_USED.getStatus());
				entityDao.update(tmpTCardDetail);
				
				//返回卡密
				resp.setCardPwd(tmpTCardDetail.getCardPwd());
				resp.setCardNo(tmpTCardDetail.getCardNo());
				
				tmpTSigninExchange.setCardNo(tmpTCardDetail.getCardNo());
				tmpTSigninExchange.setCardPwd(tmpTCardDetail.getCardPwd());
				/**
				  * 更新卡已使用数量
				  */
				TCardInfo tmpTCardInfo = tmpTCardDetail.getTcardInfo();
				if (tmpTCardInfo != null ){
					Integer tmpUseCount = tmpTCardInfo.getUseCount();
					tmpUseCount = tmpUseCount == null ? 1 : tmpUseCount + 1;
					tmpTCardInfo.setUseCount(tmpUseCount);
					entityDao.update(tmpTCardInfo);
				}
			}
			//更新奖品剩余数量
			tmpTSigninPrize.setNumber(tmpPrizeNum);
			tmpTSigninPrize.setExchangeNum(tmpExchangeNum + 1);
			entityDao.update(tmpTSigninPrize);
			
			tmpTSigninExchange.setCpCode(tmpTSignin.getCpCode());
			tmpTSigninExchange.setExchangeGold(tmpConsumeGold);
			tmpTSigninExchange.setExchangeTime(currentDate);
			tmpTSigninExchange.setPrizeCode(tmpTSigninPrize.getCode());
			tmpTSigninExchange.setPrizeName(tmpTSigninPrize.getName());
			tmpTSigninExchange.setTsignin(tmpTSignin);
			tmpTSigninExchange.setUserId(userID);
			tmpTSigninExchange.setMobile(tmpTSigninUser.getMobile());
			entityDao.saveObj(tmpTSigninExchange);
			
			logger.info("兑换成功！");
			resp.setGoldNum(tmpTSigninUser.getGoldNum());
		} catch (Exception e) {
			logger.error("兑换失败，程序异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("兑换失败，程序异常！");
		}
		return resp;
	}

	@Transactional
	public LuckySigninResp updateSigninUser(String signinCode, String userID,
			String mobileNo) {
		
		LuckySigninResp resp = new LuckySigninResp();
		if (StringUtils.isBlank(signinCode)){
			logger.error("更新签到用户失败，签到编码非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("更新签到用户失败，签到编码非法！");
			return resp;
		}
		if (StringUtils.isBlank(userID)){
			logger.error("更新签到用户失败，账号非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("更新签到用户失败，账号非法！");
			return resp;
		}
		if (StringUtils.isBlank(mobileNo)){
			logger.error("更新签到用户失败，手机号非法！");
			resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("更新签到用户失败，手机号非法！");
			return resp;
		}
		try {
			Date currentDate = new Date();
			TSignin tmpTSignin = entityDao.get(TSignin.class, "o.code=?1", new Object[]{signinCode});
			if (tmpTSignin == null){
				logger.error("更新签到用户失败，活动不存在！");
				resp.setResultCode(LuckySigninResultCode.SIGNIN_DOES_NOT_EXIST);
				resp.setResultDesc("更新签到用户失败，活动不存在！");
				return resp;
			}
			//验证签到活动有效性
			ValidateRets<TSignin> tmpTSigninValidateRets = signinValidate.validate(new SigninValidateArgs(resp, 
					tmpTSignin, currentDate));
			if (!tmpTSigninValidateRets.isSuccess()) return resp;
			
			TSigninUser tmpTSigninUser = entityDao.get(TSigninUser.class, "o.tsignin.code=?1 and o.userId=?2", new Object[]{signinCode, userID});
			if (tmpTSigninUser == null){
				logger.error("更新签到用户失败，用户不存在！");
				resp.setResultCode(LuckySigninResultCode.USER_DOES_NOT_EXIST);
				resp.setResultDesc("更新签到用户失败，用户不存在！");
				return resp;
			}
			Integer tmpStatus = tmpTSigninUser.getStatus() == null ? 0 : tmpTSigninUser.getStatus();
			if (tmpStatus == 0){
				logger.error("更新签到用户失败，用户已经停用！");
				resp.setResultCode(LuckySigninResultCode.USER_HAS_BEEN_STOPPED);
				resp.setResultDesc("更新签到用户失败，用户已经停用！");
				return resp;
			}
			
			boolean tmpUpdateMobile = true;
			String tmpMobileNo = tmpTSigninUser.getMobile();
			if (!StringUtils.isBlank(tmpMobileNo) && tmpMobileNo.equals(mobileNo)){
				tmpUpdateMobile = false;
			}
			if (tmpUpdateMobile){
				tmpTSigninUser.setMobile(mobileNo);
				tmpTSigninUser.setUpdateTime(currentDate);
				entityDao.update(tmpTSigninUser);
				
				//更新兑换记录
				List<TSigninExchange> tmpListTSigninExchange = entityDao.getScrollData(TSigninExchange.class, 
						"o.tsignin.code=?1 and o.userId=?2", new Object[]{signinCode, userID});
				if (tmpListTSigninExchange != null && !tmpListTSigninExchange.isEmpty()){
					for (TSigninExchange tmpTSigninExchange : tmpListTSigninExchange){
						tmpTSigninExchange.setMobile(mobileNo);
						entityDao.update(tmpTSigninExchange);
					}
				}
			}
			resp.setUserID(userID);
			resp.setMobile(mobileNo);
			resp.setGoldNum(tmpTSigninUser.getGoldNum());
			logger.info("更新签到用户成功！");
		} catch (Exception e) {
			logger.error("更新签到用户失败，系统异常：" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("更新签到用户失败，系统异常！");
		}
		return resp;
	}

	public GetSigninRecordResp getSigninRecord(String signinCode, String userID,
                                               String startDay, String endDay) {
		GetSigninRecordResp resp = new  GetSigninRecordResp();
		try {
			if (StringUtils.isBlank(signinCode)){
				logger.error("获取签到记录失败，签到编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，签到编码非法！");
				return resp;
			}
			if (StringUtils.isBlank(userID)){
				logger.error("获取签到记录失败，账号非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，账号非法！");
				return resp;
			}
			if (StringUtils.isBlank(startDay) || StringUtils.isBlank(endDay)){
				logger.error("获取签到记录失败，日期非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，日期非法！");
				return resp;
			}
			Date tmpStartTime;
			Date tmpEndTime;
			try{
				tmpStartTime = DateUtils.parseDate(startDay, new String[]{"yyyyMMdd"});
				tmpEndTime = DateUtils.parseDate(endDay, new String[]{"yyyyMMdd"});
			} catch (ParseException e){
				logger.error("获取签到记录失败，参数非法（要求格式yyyyMMdd）！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，参数非法（要求格式yyyyMMdd）！");
				return resp;
			}
			if (tmpStartTime.after(tmpEndTime)){
				logger.error("获取签到记录失败，开始时间大于结束时间！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，开始时间大于结束时间！");
				return resp;
			}
			TSignin tmpTSignin = entityDao.get(TSignin.class, "o.code=?1", new Object[]{signinCode});
			if (tmpTSignin == null){
				logger.error("获取签到记录失败，签到编码不存在！");
				resp.setResultCode(LuckySigninResultCode.SIGNIN_DOES_NOT_EXIST);
				resp.setResultDesc("获取签到记录失败，签到编码不存在！");
				return resp;
			}
			Date currentDate = new Date();
			//验证签到活动有效性
			ValidateRets<TSignin> tmpTSigninValidateRets = signinValidate.validate(new SigninValidateArgs(resp, 
					tmpTSignin, currentDate));
			if (!tmpTSigninValidateRets.isSuccess()) return resp;
			
			long divMillis = tmpEndTime.getTime() - tmpStartTime.getTime();
			//24 * 60 * 60 * 1000;
			long oneDayMillis = 86400000;
			int betweenDays = new Long(divMillis/oneDayMillis).intValue();
			List<GetSigninRecordResp.SigninRecordInfo> tmpListSigninRecordInfo = new ArrayList<GetSigninRecordResp.SigninRecordInfo>(); 
			GetSigninRecordResp.SigninRecordInfo tmpSigninRecordInfo;
			String tmpDay;
			if (betweenDays == 0){
				tmpSigninRecordInfo = new GetSigninRecordResp.SigninRecordInfo();
				tmpSigninRecordInfo.setSigninDay(startDay);
				tmpSigninRecordInfo.setCpCode(tmpTSignin.getCpCode());
				tmpSigninRecordInfo.setUserID(userID);
				tmpSigninRecordInfo.setSigninFlag(0);//未签到
				tmpListSigninRecordInfo.add(tmpSigninRecordInfo);
			}else{
				for (int i=0; i<=betweenDays; i++){
					tmpDay = DateFormatUtil.format(DateUtils.addDays(tmpStartTime, i), "yyyyMMdd");
					tmpSigninRecordInfo = new GetSigninRecordResp.SigninRecordInfo();
					tmpSigninRecordInfo.setSigninDay(tmpDay);
					tmpSigninRecordInfo.setCpCode(tmpTSignin.getCpCode());
					tmpSigninRecordInfo.setUserID(userID);
					tmpSigninRecordInfo.setSigninFlag(0);//未签到
					tmpListSigninRecordInfo.add(tmpSigninRecordInfo);
				}
			}
			resp.setRecordSize(tmpListSigninRecordInfo.size());
			resp.setSigninRecordInfos(tmpListSigninRecordInfo);
			resp.setStartDay(startDay);
			resp.setEndDay(endDay);
			
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("signinTime", "asc");
			List<TSigninRecord>	tmpListTSigninRecord = entityDao.getScrollData(TSigninRecord.class, -1, -1, 
						"o.tsignin.code=?1 and o.userId=?2 and date_format(o.signinTime,'%Y%m%d')>=?3 and date_format(o.signinTime,'%Y%m%d')<=?4", 
						new Object[]{signinCode, userID, startDay, endDay}, orderBy);
			if (tmpListTSigninRecord != null && !tmpListTSigninRecord.isEmpty()){
				for (GetSigninRecordResp.SigninRecordInfo tmpInfo : tmpListSigninRecordInfo){
					for (TSigninRecord tmpTSigninRecord : tmpListTSigninRecord){
						tmpDay = DateFormatUtil.format(tmpTSigninRecord.getSigninTime(), "yyyyMMdd");
						if (tmpDay.equals(tmpInfo.getSigninDay())){
							tmpInfo.setRandomStr(tmpTSigninRecord.getRandomStr());
							tmpInfo.setSigninFlag(1);//已签到
							tmpInfo.setSigninTime(DateFormatUtil.format(tmpTSigninRecord.getSigninTime(), "yyyyMMddHHmmss"));
							break;
						}
					}
				}
			}
			logger.info("获取签到记录成功！");
		} catch (Exception e) {
			logger.error("获取签到记录失败，程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取签到记录失败，程序异常！");
		}
		return resp;
		
	}
	
	public GetSigninRecordResp getCurrentDaySigninRecord(String signinCode, String userID) {
		
		GetSigninRecordResp resp = new  GetSigninRecordResp();
		try {
			if (StringUtils.isBlank(signinCode)){
				logger.error("获取当天签到记录失败，签到编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取当天签到记录失败，签到编码非法！");
				return resp;
			}
			if (StringUtils.isBlank(userID)){
				logger.error("获取当天签到记录失败，账号非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取当天签到记录失败，账号非法！");
				return resp;
			}
			Date currentDate = new Date();
			String currentDay = DateFormatUtil.format(currentDate, "yyyyMMdd");
			
			TSignin tmpTSignin = entityDao.get(TSignin.class, "o.code=?1", new Object[]{signinCode});
			if (tmpTSignin == null){
				logger.error("获取当天签到记录失败，签到编码不存在！");
				resp.setResultCode(LuckySigninResultCode.SIGNIN_DOES_NOT_EXIST);
				resp.setResultDesc("获取当天签到记录失败，签到编码不存在！");
				return resp;
			}
			
			//验证签到活动有效性
			ValidateRets<TSignin> tmpTSigninValidateRets = signinValidate.validate(new SigninValidateArgs(resp, 
					tmpTSignin, currentDate));
			if (!tmpTSigninValidateRets.isSuccess()) return resp;
			
			List<GetSigninRecordResp.SigninRecordInfo> tmpListSigninRecordInfo = new ArrayList<GetSigninRecordResp.SigninRecordInfo>(); 
			GetSigninRecordResp.SigninRecordInfo tmpSigninRecordInfo = new GetSigninRecordResp.SigninRecordInfo();
			tmpSigninRecordInfo.setSigninDay(currentDay);
			tmpSigninRecordInfo.setCpCode(tmpTSignin.getCpCode());
			tmpSigninRecordInfo.setUserID(userID);
			tmpSigninRecordInfo.setSigninFlag(0);//未签到
			tmpListSigninRecordInfo.add(tmpSigninRecordInfo);
			
			resp.setRecordSize(tmpListSigninRecordInfo.size());
			resp.setSigninRecordInfos(tmpListSigninRecordInfo);
			resp.setStartDay(currentDay);
			resp.setEndDay(currentDay);
			
			
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("signinTime", "asc");
			TSigninRecordDay tmpTSigninRecordDay = entityDao.get(TSigninRecordDay.class,"o.tsignin.code=?1 and o.userId=?2 and date_format(o.signinTime,'%Y%m%d')>=?3 and date_format(o.signinTime,'%Y%m%d')<=?4", 
						new Object[]{signinCode, userID, currentDay, currentDay});
			if (tmpTSigninRecordDay != null){
				tmpSigninRecordInfo.setRandomStr(tmpTSigninRecordDay.getRandomStr());
				tmpSigninRecordInfo.setSigninFlag(1);//已签到
				tmpSigninRecordInfo.setSigninTime(DateFormatUtil.format(tmpTSigninRecordDay.getSigninTime(), "yyyyMMddHHmmss"));
			}
			logger.info("获取当天签到记录成功！");
		} catch (Exception e) {
			logger.error("获取当天签到记录失败，程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取当天签到记录失败，程序异常！");
		}
		return resp;
	}

	public GetSigninRecordResp getSigninRecordByProductID(String signinCode,
			String productID, String userID, String startDay, String endDay) {
		GetSigninRecordResp resp = new  GetSigninRecordResp();
		try {
			if (StringUtils.isBlank(signinCode)){
				logger.error("获取签到记录失败，签到编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，签到编码非法！");
				return resp;
			}
			if (StringUtils.isBlank(userID)){
				logger.error("获取签到记录失败，账号非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，账号非法！");
				return resp;
			}
			if (StringUtils.isBlank(productID)){
				logger.error("获取签到记录失败，产品非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，产品非法！");
				return resp;
			}
			if (StringUtils.isBlank(startDay) || StringUtils.isBlank(endDay)){
				logger.error("获取签到记录失败，日期非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，日期非法！");
				return resp;
			}
			Date tmpStartTime;
			Date tmpEndTime;
			try{
				tmpStartTime = DateUtils.parseDate(startDay, new String[]{"yyyyMMdd"});
				tmpEndTime = DateUtils.parseDate(endDay, new String[]{"yyyyMMdd"});
			} catch (ParseException e){
				logger.error("获取签到记录失败，参数非法（要求格式yyyyMMdd）！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，参数非法（要求格式yyyyMMdd）！");
				return resp;
			}
			if (tmpStartTime.after(tmpEndTime)){
				logger.error("获取签到记录失败，开始时间大于结束时间！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取签到记录失败，开始时间大于结束时间！");
				return resp;
			}
			TSignin tmpTSignin = entityDao.get(TSignin.class, "o.code=?1", new Object[]{signinCode});
			if (tmpTSignin == null){
				logger.error("获取签到记录失败，签到编码不存在！");
				resp.setResultCode(LuckySigninResultCode.SIGNIN_DOES_NOT_EXIST);
				resp.setResultDesc("获取签到记录失败，签到编码不存在！");
				return resp;
			}
			
			String[] tmpArrayProductID = productID.split(",");
			Set<String> tmpSetProductID = new HashSet<String>();
			for (String tmpProductID : tmpArrayProductID){
				tmpSetProductID.add(tmpProductID);
			}
			tmpArrayProductID = new String[tmpSetProductID.size()];
			tmpArrayProductID = tmpSetProductID.toArray(tmpArrayProductID);
			
			Date currentDate = new Date();
			//验证签到活动有效性
			ValidateRets<TSignin> tmpTSigninValidateRets = signinValidate.validate(new SigninValidateArgs(resp, 
					tmpTSignin, currentDate));
			if (!tmpTSigninValidateRets.isSuccess()) return resp;
			
			List<Object> tmpListQueryParam = new ArrayList<Object>();
			tmpListQueryParam.add(signinCode);
			tmpListQueryParam.add(userID);
			tmpListQueryParam.add(startDay);
			tmpListQueryParam.add(endDay);
			StringBuilder tmpInSb = new StringBuilder("");
			
			String tmpProductID;
			for (int i = 0; i < tmpArrayProductID.length; i++){
				tmpProductID = tmpArrayProductID[i];
				tmpInSb.append("?" + (5 + i)).append(",");
				tmpListQueryParam.add(tmpProductID);
			}
			if (tmpInSb.length() > 0) tmpInSb.deleteCharAt(tmpInSb.length() -1);
			
			long divMillis = tmpEndTime.getTime() - tmpStartTime.getTime();
			//24 * 60 * 60 * 1000;
			long oneDayMillis = 86400000;
			int betweenDays = new Long(divMillis/oneDayMillis).intValue();
			List<GetSigninRecordResp.SigninRecordInfo> tmpListSigninRecordInfo = new ArrayList<GetSigninRecordResp.SigninRecordInfo>(); 
			GetSigninRecordResp.SigninRecordInfo tmpSigninRecordInfo;
			String tmpDay;
			if (betweenDays == 0){
				for (int j = 0 ; j < tmpArrayProductID.length; j++){
					tmpProductID = tmpArrayProductID[j];
					tmpSigninRecordInfo = new GetSigninRecordResp.SigninRecordInfo();
					tmpSigninRecordInfo.setSigninDay(startDay);
					tmpSigninRecordInfo.setCpCode(tmpTSignin.getCpCode());
					tmpSigninRecordInfo.setUserID(userID);
					tmpSigninRecordInfo.setSigninFlag(0);//未签到
					tmpSigninRecordInfo.setProductID(tmpProductID);
					tmpListSigninRecordInfo.add(tmpSigninRecordInfo);
				}
			}else{
				for (int i=0; i<=betweenDays; i++){
					tmpDay = DateFormatUtil.format(DateUtils.addDays(tmpStartTime, i), "yyyyMMdd");
					for (int j = 0 ; j < tmpArrayProductID.length; j++){
						tmpProductID = tmpArrayProductID[j];
						tmpSigninRecordInfo = new GetSigninRecordResp.SigninRecordInfo();
						tmpSigninRecordInfo.setSigninDay(tmpDay);
						tmpSigninRecordInfo.setCpCode(tmpTSignin.getCpCode());
						tmpSigninRecordInfo.setUserID(userID);
						tmpSigninRecordInfo.setSigninFlag(0);//未签到
						tmpSigninRecordInfo.setProductID(tmpProductID);
						tmpListSigninRecordInfo.add(tmpSigninRecordInfo);
					}
				}
			}
			resp.setRecordSize(tmpListSigninRecordInfo.size());
			resp.setSigninRecordInfos(tmpListSigninRecordInfo);
			resp.setStartDay(startDay);
			resp.setEndDay(endDay);
			
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("signinTime", "asc");
			List<TSigninRecord>	tmpListTSigninRecord = entityDao.getScrollData(TSigninRecord.class, -1, -1, 
						"o.tsignin.code=?1 and o.userId=?2 and date_format(o.signinTime,'%Y%m%d')>=?3 and date_format(o.signinTime,'%Y%m%d')<=?4 and o.productCode in (" + tmpInSb.toString() + ")", 
						tmpListQueryParam.toArray(), orderBy);
			
			if (tmpListTSigninRecord != null && !tmpListTSigninRecord.isEmpty()){
				for (GetSigninRecordResp.SigninRecordInfo tmpInfo : tmpListSigninRecordInfo){
					for (TSigninRecord tmpTSigninRecord : tmpListTSigninRecord){
						tmpDay = DateFormatUtil.format(tmpTSigninRecord.getSigninTime(), "yyyyMMdd");
						tmpProductID = tmpTSigninRecord.getProductCode();
						if (tmpDay.equals(tmpInfo.getSigninDay()) && tmpProductID.equals(tmpInfo.getProductID())){
							tmpInfo.setRandomStr(tmpTSigninRecord.getRandomStr());
							tmpInfo.setSigninFlag(1);//已签到
							tmpInfo.setSigninTime(DateFormatUtil.format(tmpTSigninRecord.getSigninTime(), "yyyyMMddHHmmss"));
							break;
						}
					}
				}
			}
			logger.info("获取签到记录成功！");
		} catch (Exception e) {
			logger.error("获取签到记录失败，程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取签到记录失败，程序异常！");
		}
		return resp;
	}

	public GetSigninRecordResp getCurrentDaySigninRecordByProductID(
			String signinCode, String productID, String userID) {
		
		GetSigninRecordResp resp = new  GetSigninRecordResp();
		try {
			if (StringUtils.isBlank(signinCode)){
				logger.error("获取当天签到记录失败，签到编码非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取当天签到记录失败，签到编码非法！");
				return resp;
			}
			if (StringUtils.isBlank(userID)){
				logger.error("获取当天签到记录失败，账号非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取当天签到记录失败，账号非法！");
				return resp;
			}
			if (StringUtils.isBlank(productID)){
				logger.error("获取当天签到记录失败，产品非法！");
				resp.setResultCode(LuckySigninResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("获取当天签到记录失败，产品非法！");
				return resp;
			}
			TSignin tmpTSignin = entityDao.get(TSignin.class, "o.code=?1", new Object[]{signinCode});
			if (tmpTSignin == null){
				logger.error("获取签到记录失败，签到编码不存在！");
				resp.setResultCode(LuckySigninResultCode.SIGNIN_DOES_NOT_EXIST);
				resp.setResultDesc("获取签到记录失败，签到编码不存在！");
				return resp;
			}
			
			String[] tmpArrayProductID = productID.split(",");
			Set<String> tmpSetProductID = new HashSet<String>();
			for (String tmpProductID : tmpArrayProductID){
				tmpSetProductID.add(tmpProductID);
			}
			tmpArrayProductID = new String[tmpSetProductID.size()];
			tmpArrayProductID = tmpSetProductID.toArray(tmpArrayProductID);
			
			Date currentDate = new Date();
			String currentDay = DateFormatUtil.format(currentDate, "yyyyMMdd");
			//验证签到活动有效性
			ValidateRets<TSignin> tmpTSigninValidateRets = signinValidate.validate(new SigninValidateArgs(resp, 
					tmpTSignin, currentDate));
			if (!tmpTSigninValidateRets.isSuccess()) return resp;
			
			List<Object> tmpListQueryParam = new ArrayList<Object>();
			tmpListQueryParam.add(signinCode);
			tmpListQueryParam.add(userID);
			tmpListQueryParam.add(currentDay);
			tmpListQueryParam.add(currentDay);
			
			List<GetSigninRecordResp.SigninRecordInfo> tmpListSigninRecordInfo = new ArrayList<GetSigninRecordResp.SigninRecordInfo>(); 
			GetSigninRecordResp.SigninRecordInfo tmpSigninRecordInfo;
			String tmpDay;
			String tmpProductID;
			StringBuilder tmpInSb = new StringBuilder("");
			for (int j = 0 ; j < tmpArrayProductID.length; j++){
				tmpProductID = tmpArrayProductID[j];
				tmpSigninRecordInfo = new GetSigninRecordResp.SigninRecordInfo();
				tmpSigninRecordInfo.setSigninDay(currentDay);
				tmpSigninRecordInfo.setCpCode(tmpTSignin.getCpCode());
				tmpSigninRecordInfo.setUserID(userID);
				tmpSigninRecordInfo.setSigninFlag(0);//未签到
				tmpSigninRecordInfo.setProductID(tmpProductID);
				tmpListSigninRecordInfo.add(tmpSigninRecordInfo);
				
				tmpInSb.append("?" + (5 + j)).append(",");
				tmpListQueryParam.add(tmpProductID);
			}
			if (tmpInSb.length() > 0) tmpInSb.deleteCharAt(tmpInSb.length() -1);
			
			resp.setRecordSize(tmpListSigninRecordInfo.size());
			resp.setSigninRecordInfos(tmpListSigninRecordInfo);
			resp.setStartDay(currentDay);
			resp.setEndDay(currentDay);
			
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("signinTime", "asc");
			List<TSigninRecordDay> tmpListTSigninRecordDay = entityDao.getScrollData(TSigninRecordDay.class, -1, -1, 
						"o.tsignin.code=?1 and o.userId=?2 and date_format(o.signinTime,'%Y%m%d')>=?3 and date_format(o.signinTime,'%Y%m%d')<=?4 and o.productCode in (" + tmpInSb.toString() + ")", 
						tmpListQueryParam.toArray(), orderBy);
			
			if (tmpListTSigninRecordDay != null && !tmpListTSigninRecordDay.isEmpty()){
				for (GetSigninRecordResp.SigninRecordInfo tmpInfo : tmpListSigninRecordInfo){
					for (TSigninRecordDay tmpTSigninRecordDay : tmpListTSigninRecordDay){
						tmpDay = DateFormatUtil.format(tmpTSigninRecordDay.getSigninTime(), "yyyyMMdd");
						tmpProductID = tmpTSigninRecordDay.getProductCode();
						if (tmpDay.equals(tmpInfo.getSigninDay()) && tmpProductID.equals(tmpInfo.getProductID())){
							tmpInfo.setRandomStr(tmpTSigninRecordDay.getRandomStr());
							tmpInfo.setSigninFlag(1);//已签到
							tmpInfo.setSigninTime(DateFormatUtil.format(tmpTSigninRecordDay.getSigninTime(), "yyyyMMddHHmmss"));
							break;
						}
					}
				}
			}
			logger.info("获取当天签到记录成功！");
		} catch (Exception e) {
			logger.error("获取当天签到记录失败，程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckySigninResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("获取当天签到记录失败，程序异常！");
		}
		return resp;
	}
}

